package com.scenery;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.Projection;
import com.scenery.PoiSearch.SceneryOverlay;
import com.scenery.vo.Scenery;
import com.scenery.vo.User;
/***
 * 用户地图
 * **/
public class UserFriendMapActivity extends MapActivity {
	private String tag=UserFriendMapActivity.class.getName();

	MapView mMapView = null;	// 地图View
	MKSearch mSearch = null;	// 搜索模块，也可去掉地图模块独立使用
	

	
	
	EditText startEdit,endEdit;
	ArrayList<User> userList;
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
        setContentView(R.layout.user_map);
       Log.d(tag, "onCreate");
        init();
        
	}
	
	@Override
	protected void onPause() {
		BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
		app.mBMapMan.stop();
		super.onPause();
	}
	@Override
	protected void onResume() {
		BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
		app.mBMapMan.start();
		super.onResume();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	/****
	 * 界面初始化
	 * **/
	private void init()
	{
		Log.d(tag, "init");
		BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
		if (app.mBMapMan == null) {
			app.mBMapMan = new BMapManager(getApplication());
			app.mBMapMan.init(app.mStrKey, new BMapApiDemoApp.MyGeneralListener());
		}
		app.mBMapMan.start();
        // 如果使用地图SDK，请初始化地图Activity
        super.initMapActivity(app.mBMapMan);
        
        mMapView = (MapView)findViewById(R.id.friendMapView);
       
       
        
       
        
        mMapView.setBuiltInZoomControls(true);
        //设置在缩放动画过程中也显示overlay,默认为不绘制
        mMapView.setDrawOverlayWhenZooming(true);
        
        // 初始化搜索模块，注册事件监听
      
        
        // 设定搜索按钮的响应
     
        
      
       
        
        getuserList();
        Drawable marker = getResources().getDrawable(R.drawable.iconmarka);  //得到需要标在地图上的资源
        UserFirendOverlay so=new UserFirendOverlay(marker, UserFriendMapActivity.this,userList);
        mMapView.getOverlays().add(new UserFirendOverlay(marker, UserFriendMapActivity.this,userList)); //添加ItemizedOverlay实例到mMapView
        if(so.getGeoList().size()>0)
        {
        	mMapView.getController().animateTo(so.getGeoList().get(0).getPoint());
        }
    	
	}
	
	
	
	
	 private void getuserList() {
		// TODO Auto-generated method stub
		 Log.d(tag, "getuserList");
		 Bundle bundle = this.getIntent().getExtras();
			userList=bundle.getParcelableArrayList("list");
			Log.d(tag,"sceneryList.size():"+ userList.size());
	}




	class UserFirendOverlay extends ItemizedOverlay<OverlayItem> {
			private Context mContext;
			private List<OverlayItem> geoList = new ArrayList<OverlayItem>();
			private ArrayList<User> list=new  ArrayList<User>();
			String tag=SceneryOverlay.class.getName();
			private Drawable marker;
			
			
			public List<OverlayItem> getGeoList() {
				return geoList;
			}
			
			public UserFirendOverlay(Drawable marker, Context context, ArrayList<User> sceneryList) {
				  super(boundCenterBottom(marker));
				  Log.d(tag, "SceneryOverlay");
				  this.marker=marker;
				  this.mContext = context;
				  this.list=sceneryList;
				  // TODO Auto-generated constructor stub
				  init();
				 
			}
			public void init()
			{
				Log.d(tag, "init");
				Log.d(tag, "init sceneryList.size():"+userList.size());
				for(int i=0;i<userList.size();i++)
				{
					User user=userList.get(i);
					Log.d(tag, "用户坐标："+user.getLat()+","+user.getLon());
					int lat=(int) (Double.parseDouble(user.getLat()) * 1E6);
					
					int lon=(int) (Double.parseDouble(user.getLon()) * 1E6);
					Log.d(tag, "用户坐标："+lat+","+lon);
					GeoPoint p1 = new GeoPoint( lat, lon);
					//GeoPoint p1 = new GeoPoint( lon, lat);
					geoList.add(new OverlayItem(p1, user.getUserId(), ""));
				}
				 populate();  //createItem(int)方法构造item。一旦有了数据，在调用其它方法前，首先调用这个方法
			}
			@Override
			public void draw(Canvas canvas, MapView mapView, boolean shadow) {
				Log.d(tag, "draw");
				// Projection接口用于屏幕像素坐标和经纬度坐标之间的变换
				Projection projection = mapView.getProjection(); 
				for (int index = size() - 1; index >= 0; index--) { // 遍历mGeoList
					OverlayItem overLayItem = getItem(index); // 得到给定索引的item

					String title = overLayItem.getTitle();
					// 把经纬度变换到相对于MapView左上角的屏幕像素坐标
					Point point = projection.toPixels(overLayItem.getPoint(), null); 

					// 可在此处添加您的绘制代码
					Paint paintText = new Paint();
					paintText.setColor(Color.BLUE);
					paintText.setTextSize(15);
					canvas.drawText(title, point.x-30, point.y, paintText); // 绘制文本
				}

				super.draw(canvas, mapView, shadow);
				//调整一个drawable边界，使得（0，0）是这个drawable底部最后一行中心的一个像素
				boundCenterBottom(marker);
			}
			@Override
			protected OverlayItem createItem(int arg0) {
				// TODO Auto-generated method stub
				Log.d(tag, "createItem");
				return geoList.get(arg0);
			}

			@Override
			public int size() {
				// TODO Auto-generated method stub
				Log.d(tag, "createItem");
				return geoList.size();
			}

			@Override
			protected boolean onTap(int i) {
				// TODO Auto-generated method stub
			//	return super.onTap(arg0);
				Log.d(tag, "onTap");

				final int idex=i;
				AlertDialog dialog=new AlertDialog.Builder(UserFriendMapActivity.this).create();
				dialog.setMessage(list.get(idex).getUserId());
				dialog.setButton("关闭", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
						
					}
				});
				dialog.show();
				return true;
			}

		}
}
